package com.yjps.system.filter;

import com.yjps.system.model.User;
import org.apache.shiro.web.filter.authz.AuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by pcc on 2015/8/18.
 */
public class CheckLoginFilter extends AuthorizationFilter {
    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        return false;
    }

    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws IOException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        User loginUser = (User)httpRequest.getSession().getAttribute("loginUser");
        System.out.println("filter....");
        //拦截
        if (loginUser != null) {
            return true;
        }else{
            //该用户还没登录
            System.out.println("have not login....");
            saveRequestAndRedirectToLogin(request, response);
        }
        return false;
    }
}
